package com.company.cloud.user.modules.app.service.impl;

import java.util.List;

import com.company.common.server.config.cloud.ProjectConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.user.constant.ErrorUserEnum;
import com.company.cloud.user.constant.UserConstant;
import com.company.cloud.user.constant.UserMsgSendTypeEnum;
import com.company.cloud.user.modules.app.models.SendVerificationCodeDto;
import com.company.cloud.user.modules.app.service.VCodeSmsService;
import com.company.cloud.user.modules.app.service.UserCodeService;
import com.company.cloud.user.modules.app.utils.UserVCodeUtils;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.log4j.Log4j2;

@Log4j2
@Service
public class UserCodeServiceImpl implements UserCodeService {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ProjectConfig projectConfig;
    @Autowired
    private VCodeSmsService smsService;


    /**
     * 验证验证码
     */
    @Override
    public void validate(String companyNo, UserMsgSendTypeEnum sendType, String mobile, String vcode, boolean isOkDel) {
        String vkey = UserVCodeUtils.getVCodeKey(companyNo, sendType, mobile);
        System.out.println("VCODE=" + vkey);
        this.validate(vkey, vcode, isOkDel);
    }

    /**
     * 验证验证码
     */
    @Override
    public void validate(String vkey, String vcode, boolean isOkDel) {
        List<String> mobiles = StrSplitUtils.parse2list(projectConfig.getVCodeMobile(), String.class);
        if (!CollectionUtils.isEmpty(mobiles)) {
            for (String mobile : mobiles) {
                if (vkey.contains(mobile) && vcode.equals(projectConfig.getVCodeDef())) {
                    return;
                }
            }
        }
        String code = "";
        Object _codeObj = redisUtils.mget(vkey, vcode);
        if (_codeObj != null) {
            code = _codeObj.toString();
        }
        if (code == null || "".equals(code)) {
            throw new BusinessException(ErrorUserEnum.VCode_User_Expire);
        }
        if (!code.equals(vcode)) {
            throw new BusinessException(ErrorUserEnum.VCode_User_Error);
        }
        if (isOkDel) {
            redisUtils.mdel(vkey, vcode);
        }
    }

    /**
     * 删除验证码
     */
    @Override
    public void delVCode(String companyNo, UserMsgSendTypeEnum sendType, String mobile) {
        String vkey = UserVCodeUtils.getVCodeKey(companyNo, sendType, mobile);
        this.delVCode(vkey);
    }


    /**
     * 删除验证码
     */
    @Override
    public void delVCode(String vkey) {
        redisUtils.delete(vkey);
    }

    /**
     * 发送验证码
     */
    @Override
    public String sendCodeByCompany(String companyNo, UserMsgSendTypeEnum sendType, String mobile) {
        return this.sendCode(companyNo, sendType, mobile, 4, 10);
    }


    /**
     * 发送验证码
     */
    @Override
    public String sendCodeByPlatform(UserMsgSendTypeEnum sendType, String mobile) {
        return this.sendCode(UserConstant.Platform_VCode_Com_Def, sendType, mobile, 4, 10);
    }


    /**
     * 发送验证码
     */
    @Override
    public String sendCode(String companyNo, UserMsgSendTypeEnum sendType, String mobile, int len, int expireSecond) {
        String vkey = UserVCodeUtils.getVCodeKey(companyNo, sendType, mobile);

        System.out.println("Send VCode =" + vkey);

        //生成新的验证码
        String vcode = this.getVCode(mobile, len);
        redisUtils.mput(vkey, vcode, vcode, 60 * expireSecond);

        // 白名单
        List<String> mobiles = StrSplitUtils.parse2list(projectConfig.getVCodeMobile(), String.class);
        if (!CollectionUtils.isEmpty(mobiles)) {
            vcode = projectConfig.getVCodeDef();
        }

        //发送
        if (projectConfig.isSMSSend()) {
            log.debug("发送验证码");
            SendVerificationCodeDto sendVerificationCodeDto = new SendVerificationCodeDto();
            sendVerificationCodeDto.setMobile(mobile);
            sendVerificationCodeDto.setVerificationCode(vcode);
            sendVerificationCodeDto.setType(sendType.getCode());
            smsService.sendVerificationCode(sendVerificationCodeDto); // 发送
        } else {

        }
        return vkey;
    }

    /***/
    private String getVCode(String mobile, int len) {
        //判断是否在白名单
        if (!StringUtils.isEmpty(projectConfig.getVCodeMobile())) {
            List<String> mobiles = StrSplitUtils.parse2list(projectConfig.getVCodeMobile(), String.class);
            if (!CollectionUtils.isEmpty(mobiles)) {
                if (mobiles.contains(mobile)) {
                    return projectConfig.getVCodeDef();
                }
            }
        }
        if (projectConfig.isGenerateVCode()) {
            return RandomUtil.randomNumbers(len);
        } else {
            return projectConfig.getVCodeDef();
        }
    }

//	private String getSmsContent(UserMsgSendTypeEnum sendType){
//		String content = "验证码 %s (请勿告知他人)，该验证码是{}，10分钟有效。【太平经纪】";
//		return StrUtil.format(content, sendType.getMsg());
//	}


}
